package com.jinke.api.common.constraint;

import org.bouncycastle.crypto.digests.*;
import org.bouncycastle.util.encoders.Hex;

import java.util.Random;

/**
 * @projectName:
 * @title: EncryptType
 * @description:
 * @see:
 * @author: hhp
 * @version: 1.0.0
 * @createTime: 2024/8/23 14:48
 */
public enum EncryptType {
    MD5,SHA256,SHA348,SM3,RIPEMD160,SHA224,SHA512,SHA384;

    public EncryptType random() {
        return random(values());
    }

    public EncryptType random(EncryptType... includes) {
        return includes[new Random().nextInt(includes.length)];
    }

    public byte[] bytes(String content) {
        if (content == null || content.isEmpty()) {
            throw new IllegalArgumentException();
        }
        byte[] bytes = content.getBytes();
        byte[] bytes1 = null;
        switch (this) {
            case MD5:
                MD5Digest md5Digest = new MD5Digest();
                md5Digest.update(bytes, 0, bytes.length);
                bytes1 = new byte[md5Digest.getDigestSize()];
                md5Digest.doFinal(bytes1, 0);
                break;
            case RIPEMD160:
                RIPEMD160Digest ripemd160Digest = new RIPEMD160Digest();
                ripemd160Digest.update(bytes, 0, bytes.length);
                bytes1 = new byte[ripemd160Digest.getDigestSize()];
                ripemd160Digest.doFinal(bytes1, 0);
                break;
            case SHA224:
                SHA224Digest sha224Digest = new SHA224Digest();
                sha224Digest.update(bytes, 0, bytes.length);
                bytes1 = new byte[sha224Digest.getDigestSize()];
                sha224Digest.doFinal(bytes1, 0);
                break;
            case SHA256:
                SHA256Digest sha256Digest = new SHA256Digest();
                sha256Digest.update(bytes, 0, bytes.length);
                bytes1 = new byte[sha256Digest.getDigestSize()];
                sha256Digest.doFinal(bytes1, 0);
                break;
            case SHA384:
                SHA384Digest sha384Digest = new SHA384Digest();
                sha384Digest.update(bytes, 0, bytes.length);
                bytes1 = new byte[sha384Digest.getDigestSize()];
                sha384Digest.doFinal(bytes1, 0);
                break;
            case SHA512:
                SHA512Digest sha512Digest = new SHA512Digest();
                sha512Digest.update(bytes, 0, bytes.length);
                bytes1 = new byte[sha512Digest.getDigestSize()];
                sha512Digest.doFinal(bytes1, 0);
                break;
            case SM3:
                SM3Digest sm3Digest = new SM3Digest();
                sm3Digest.update(bytes, 0, bytes.length);
                bytes1 = new byte[sm3Digest.getDigestSize()];
                sm3Digest.doFinal(bytes1, 0);
                break;
            default:
                break;
        }
        return bytes1;
    }

    public String of(String content) {
        return Hex.toHexString(bytes(content));
    }
}
